// 定义helper工具
layui.define(function (exports) {

    var helper = {}

    helper.hasKey = function (obj, key) {
        return obj.hasOwnProperty(key)
    }

    helper.emptyKey = function (obj, key) {
        return !obj.hasOwnProperty(key) || (obj[key] === '' || obj[key] === null);
    }

    helper.hasKeyFnt = function (obj, key, callback) {
        if (obj.hasOwnProperty(key)) callback(obj, key)
    }

    helper.getValue = function (obj, key, defaultValue) {
        if (this.hasKey(obj, key)) return obj[key];
        else return defaultValue;
    }

    helper.removeValue = function (obj, key, defaultValue) {
        if (this.hasKey(obj, key)) {
            var v = obj[key];
            delete obj[key];
            return v;
        } else {
            return defaultValue;
        }
    }

    helper.urlEncode = function (param) {
        if (param == null) return '';
        var arr = [];
        layui.each(param, function (k, v) {
            arr.push(k + '=' + v)
        });
        return arr.join('&');
    }

    helper.templateReplace = function (template, obj) {
      if (!template) {
        return null;
      }
      return template.replace(/\{\{(\w+)\}\}/g, (match, key) => obj[key] ?? match);
    }

    exports('helper', helper);

})